package com.scs.application.modules.wm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.core.query.QueryContext;
import com.scs.application.core.query.QueryContextHolder;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.modules.base.annotation.MatrCanPass;
import com.scs.application.modules.sys.model.PageResult;
import com.scs.application.modules.sys.model.RestResult;
import com.scs.application.modules.wm.dto.TransferDTO;
import com.scs.application.modules.wm.entity.Stock;
import com.scs.application.modules.wm.entity.TransferItem;
import com.scs.application.modules.wm.enums.UniqueCodeType;
import com.scs.application.modules.wm.mapper.TransferItemMapper;
import com.scs.application.modules.wm.service.StockService;
import com.scs.application.modules.wm.service.TransferItemService;
import com.scs.application.modules.wm.utils.ServiceState;
import com.scs.application.modules.wm.utils.UniqueCodeUtils;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/wm/transfer_item")
public class TransferItemController extends CrudController<TransferItemService, TransferItem> {


    @Autowired
    StockService stockService;
    @Autowired
    @Lazy
    TransferItemMapper transferItemMapper;


    @PostMapping("/scan")
    synchronized public RestResult addByScan(@RequestBody @Valid TransferDTO transferDTO) {
        // 判断唯一码是否为SN，如果是SN，则查询对应的RFID,如果RFID存在，则通过RFID去找对应的SN
        if (UniqueCodeUtils.getUniqueCodeType(transferDTO.getUniqueCode()) == UniqueCodeType.SN) {
            Stock stock = stockService.getStockByUniqueCodeWithBusKeyValue(transferDTO.getUniqueCode(),transferDTO.getTransferId());
            List<String> sns = Lists.newArrayList();
            if (StringUtils.isNotBlank((stock.getRfid()))) {
                List<Stock> stockList = stockService.getStockListByUniqueCode(stock.getRfid());
                sns = stockList.stream().map(Stock::getSn).collect(Collectors.toList());
            } else {
                sns.add(stock.getSn());
            }
            service.addTransferItems(transferDTO.getTransferId(), sns, null);
        }

        if (UniqueCodeUtils.getUniqueCodeType(transferDTO.getUniqueCode()) == UniqueCodeType.RFID) {
            List<Stock> stockList = stockService.getStockListByUniqueCode(transferDTO.getUniqueCode());
            List<String> sns = stockList.stream().map(Stock::getSn).collect(Collectors.toList());
            service.addTransferItems(transferDTO.getTransferId(), sns, transferDTO.getUniqueCode());
        }
        return RestResult.ok("拣货成功");
    }

    /**
     * 手动调拨-保存调拨明细
     **/
    @PostMapping("/addBySelect")
    @MatrCanPass(businessType = "transfer")
    synchronized public RestResult addBySelect(@RequestBody @Valid List<TransferItem> transferItemList) {
        if (transferItemList.isEmpty()) {
            return RestResult.error("数据为空");
        }
        String transferId = transferItemList.get(0).getTransferId();

        ServiceState.set("itemIdReturnFromList",transferItemList.stream().map(TransferItem::getItemIdReturnFrom).collect(Collectors.toList()));
        service.addTransferItems(transferId, transferItemList.stream().map(TransferItem::getSn).collect(Collectors.toList()), null);
        // 添加自定义提示
        if (ServiceState.getString(ServiceState.VALID_MSG) != null) {
            return RestResult.ok(ServiceState.getString(ServiceState.VALID_MSG));
        }
        return RestResult.ok();
    }

    /*
     *撤销拣货
     * */
    @PostMapping("/cancelTransferPick/{id}")
    public RestResult cancelTransferPick(@PathVariable("id") String transferId) {
        service.cancelTransferPick(transferId);
        return RestResult.ok("撤销成功");
    }

    /**
     *  冲红选择耗材列表
     * @return
     */
    @GetMapping("/pageForRed")
    public PageResult pageForRed() {
        QueryContext queryContext = QueryContextHolder.currentContext();
        IPage pageResult = service.pageForRed(queryContext);
        return PageResult.ok("查询成功", pageResult.getRecords(), pageResult.getTotal());
    }

    @GetMapping("/getPDAList")
    public RestResult getPDAList(@RequestParam("transferId") String transferId) {
        return RestResult.ok("获取成功", service.getPDAList(transferId));
    }

}
